
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>HTML5 History 模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="redirect-and-alias.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../installation.html">
            
                <a href="../installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="getting-started.html">
            
                <a href="getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="dynamic-matching.html">
            
                <a href="dynamic-matching.html">
            
                    
                    动态路由匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="nested-routes.html">
            
                <a href="nested-routes.html">
            
                    
                    嵌套路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="navigation.html">
            
                <a href="navigation.html">
            
                    
                    编程式导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="named-routes.html">
            
                <a href="named-routes.html">
            
                    
                    命名路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="named-views.html">
            
                <a href="named-views.html">
            
                    
                    命名视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="redirect-and-alias.html">
            
                <a href="redirect-and-alias.html">
            
                    
                    重定向 和 别名
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.8" data-path="history-mode.html">
            
                <a href="history-mode.html">
            
                    
                    HTML5 History 模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    进阶
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../advanced/navigation-guards.html">
            
                <a href="../advanced/navigation-guards.html">
            
                    
                    导航钩子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../advanced/meta.html">
            
                <a href="../advanced/meta.html">
            
                    
                    路由元信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../advanced/transitions.html">
            
                <a href="../advanced/transitions.html">
            
                    
                    过渡动效
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../advanced/data-fetching.html">
            
                <a href="../advanced/data-fetching.html">
            
                    
                    数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../advanced/scroll-behavior.html">
            
                <a href="../advanced/scroll-behavior.html">
            
                    
                    滚动行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../advanced/lazy-loading.html">
            
                <a href="../advanced/lazy-loading.html">
            
                    
                    懒加载
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    API 文档
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../api/router-link.html">
            
                <a href="../api/router-link.html">
            
                    
                    router-link
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../api/router-view.html">
            
                <a href="../api/router-view.html">
            
                    
                    router-view
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../api/route-object.html">
            
                <a href="../api/route-object.html">
            
                    
                    路由信息对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../api/options.html">
            
                <a href="../api/options.html">
            
                    
                    Router 构造配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../api/router-instance.html">
            
                <a href="../api/router-instance.html">
            
                    
                    Router 实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../api/component-injections.html">
            
                <a href="../api/component-injections.html">
            
                    
                    对组件注入
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >HTML5 History 模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="html5-history-&#x6A21;&#x5F0F;">HTML5 History &#x6A21;&#x5F0F;</h1>
<p><code>vue-router</code> &#x9ED8;&#x8BA4; hash &#x6A21;&#x5F0F; &#x2014;&#x2014; &#x4F7F;&#x7528; URL &#x7684; hash &#x6765;&#x6A21;&#x62DF;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684; URL&#xFF0C;&#x4E8E;&#x662F;&#x5F53; URL &#x6539;&#x53D8;&#x65F6;&#xFF0C;&#x9875;&#x9762;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x52A0;&#x8F7D;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E0D;&#x60F3;&#x8981;&#x5F88;&#x4E11;&#x7684; hash&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x8DEF;&#x7531;&#x7684; <strong>history &#x6A21;&#x5F0F;</strong>&#xFF0C;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x5145;&#x5206;&#x5229;&#x7528; <code>history.pushState</code> API &#x6765;&#x5B8C;&#x6210; URL &#x8DF3;&#x8F6C;&#x800C;&#x65E0;&#x987B;&#x91CD;&#x65B0;&#x52A0;&#x8F7D;&#x9875;&#x9762;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> VueRouter({
  mode: <span class="hljs-string">&apos;history&apos;</span>,
  routes: [...]
})
</code></pre>
<p>&#x5F53;&#x4F60;&#x4F7F;&#x7528; history &#x6A21;&#x5F0F;&#x65F6;&#xFF0C;URL &#x5C31;&#x50CF;&#x6B63;&#x5E38;&#x7684; url&#xFF0C;&#x4F8B;&#x5982; <code>http://yoursite.com/user/id</code>&#xFF0C;&#x4E5F;&#x597D;&#x770B;&#xFF01;</p>
<p>&#x4E0D;&#x8FC7;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x8981;&#x73A9;&#x597D;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x540E;&#x53F0;&#x914D;&#x7F6E;&#x652F;&#x6301;&#x3002;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x662F;&#x4E2A;&#x5355;&#x9875;&#x5BA2;&#x6237;&#x7AEF;&#x5E94;&#x7528;&#xFF0C;&#x5982;&#x679C;&#x540E;&#x53F0;&#x6CA1;&#x6709;&#x6B63;&#x786E;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x5F53;&#x7528;&#x6237;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x76F4;&#x63A5;&#x8BBF;&#x95EE; <code>http://oursite.com/user/id</code> &#x5C31;&#x4F1A;&#x8FD4;&#x56DE; 404&#xFF0C;&#x8FD9;&#x5C31;&#x4E0D;&#x597D;&#x770B;&#x4E86;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x5462;&#xFF0C;&#x4F60;&#x8981;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#x589E;&#x52A0;&#x4E00;&#x4E2A;&#x8986;&#x76D6;&#x6240;&#x6709;&#x60C5;&#x51B5;&#x7684;&#x5019;&#x9009;&#x8D44;&#x6E90;&#xFF1A;&#x5982;&#x679C; URL &#x5339;&#x914D;&#x4E0D;&#x5230;&#x4EFB;&#x4F55;&#x9759;&#x6001;&#x8D44;&#x6E90;&#xFF0C;&#x5219;&#x5E94;&#x8BE5;&#x8FD4;&#x56DE;&#x540C;&#x4E00;&#x4E2A; <code>index.html</code> &#x9875;&#x9762;&#xFF0C;&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#x5C31;&#x662F;&#x4F60; app &#x4F9D;&#x8D56;&#x7684;&#x9875;&#x9762;&#x3002;</p>
<h2 id="&#x540E;&#x7AEF;&#x914D;&#x7F6E;&#x4F8B;&#x5B50;">&#x540E;&#x7AEF;&#x914D;&#x7F6E;&#x4F8B;&#x5B50;</h2>
<h4 id="apache">Apache</h4>
<pre><code class="lang-apache"><span class="hljs-section">&lt;IfModule mod_rewrite.c&gt;</span>
  <span class="hljs-attribute"><span class="hljs-nomarkup">RewriteEngine</span></span> <span class="hljs-literal">On</span>
  <span class="hljs-attribute">RewriteBase</span> /
  <span class="hljs-attribute"><span class="hljs-nomarkup">RewriteRule</span></span> ^index\.html$ -<span class="hljs-meta"> [L]</span>
  <span class="hljs-attribute"><span class="hljs-nomarkup">RewriteCond</span></span> <span class="hljs-variable">%{REQUEST_FILENAME}</span> !-f
  <span class="hljs-attribute"><span class="hljs-nomarkup">RewriteCond</span></span> <span class="hljs-variable">%{REQUEST_FILENAME}</span> !-d
  <span class="hljs-attribute"><span class="hljs-nomarkup">RewriteRule</span></span> . /index.html<span class="hljs-meta"> [L]</span>
<span class="hljs-section">&lt;/IfModule&gt;</span>
</code></pre>
<h4 id="nginx">nginx</h4>
<pre><code class="lang-nginx"><span class="hljs-attribute">location</span> / {
  <span class="hljs-attribute">try_files</span> <span class="hljs-variable">$uri</span> <span class="hljs-variable">$uri</span>/ /index.html;
}
</code></pre>
<h4 id="nodejs-express">Node.js (Express)</h4>
<p><a href="https://github.com/bripkens/connect-history-api-fallback" target="_blank">https://github.com/bripkens/connect-history-api-fallback</a></p>
<h2 id="&#x8B66;&#x544A;">&#x8B66;&#x544A;</h2>
<p>&#x7ED9;&#x4E2A;&#x8B66;&#x544A;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E48;&#x505A;&#x4EE5;&#x540E;&#xFF0C;&#x4F60;&#x7684;&#x670D;&#x52A1;&#x5668;&#x5C31;&#x4E0D;&#x518D;&#x8FD4;&#x56DE; 404 &#x9519;&#x8BEF;&#x9875;&#x9762;&#xFF0C;&#x56E0;&#x4E3A;&#x5BF9;&#x4E8E;&#x6240;&#x6709;&#x8DEF;&#x5F84;&#x90FD;&#x4F1A;&#x8FD4;&#x56DE; <code>index.html</code> &#x6587;&#x4EF6;&#x3002;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x5728; Vue &#x5E94;&#x7528;&#x91CC;&#x9762;&#x8986;&#x76D6;&#x6240;&#x6709;&#x7684;&#x8DEF;&#x7531;&#x60C5;&#x51B5;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A; 404 &#x9875;&#x9762;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> VueRouter({
  mode: <span class="hljs-string">&apos;history&apos;</span>,
  routes: [
    { path: <span class="hljs-string">&apos;*&apos;</span>, component: NotFoundComponent }
  ]
})
</code></pre>
<p>&#x6216;&#x8005;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x662F;&#x7528; Node.js &#x4F5C;&#x540E;&#x53F0;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x8DEF;&#x7531;&#x6765;&#x5339;&#x914D; URL&#xFF0C;&#x5F53;&#x6CA1;&#x6709;&#x5339;&#x914D;&#x5230;&#x8DEF;&#x7531;&#x7684;&#x65F6;&#x5019;&#x8FD4;&#x56DE; 404&#xFF0C;&#x4ECE;&#x800C;&#x5B9E;&#x73B0; fallback&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="redirect-and-alias.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 重定向 和 别名">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"HTML5 History 模式","level":"1.3.8","depth":2,"next":{"title":"进阶","level":"1.4","depth":1,"ref":"","articles":[{"title":"导航钩子","level":"1.4.1","depth":2,"path":"advanced/navigation-guards.md","ref":"advanced/navigation-guards.md","articles":[]},{"title":"路由元信息","level":"1.4.2","depth":2,"path":"advanced/meta.md","ref":"advanced/meta.md","articles":[]},{"title":"过渡动效","level":"1.4.3","depth":2,"path":"advanced/transitions.md","ref":"advanced/transitions.md","articles":[]},{"title":"数据获取","level":"1.4.4","depth":2,"path":"advanced/data-fetching.md","ref":"advanced/data-fetching.md","articles":[]},{"title":"滚动行为","level":"1.4.5","depth":2,"path":"advanced/scroll-behavior.md","ref":"advanced/scroll-behavior.md","articles":[]},{"title":"懒加载","level":"1.4.6","depth":2,"path":"advanced/lazy-loading.md","ref":"advanced/lazy-loading.md","articles":[]}]},"previous":{"title":"重定向 和 别名","level":"1.3.7","depth":2,"path":"essentials/redirect-and-alias.md","ref":"essentials/redirect-and-alias.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"essentials/history-mode.md","mtime":"2017-03-04T12:16:27.893Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T16:00:44.406Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

